package database.databaseAccessObjects;


import database.DatabaseHelpers.UserConstants;
import database.DatabaseHelpers.VIPmeSQLiteHelper;
import database.Models.User;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAOUser {
	
	

	private SQLiteDatabase database;
	private VIPmeSQLiteHelper dbHelper;
	
	/**
	 * The adapter constructor. 
	 * @param context
	 */
	public DAOUser(Context context) {
		dbHelper = new VIPmeSQLiteHelper(context);
	}
	
	/**
	 * Creates the database helper and gets the database. 
	 * 
	 * @return
	 * @throws SQLException
	 */
	public DAOUser open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	/**
	 * Closes the database.
	 */
	public void close() {
		dbHelper.close();
	}
	
	/**
	 * Creates the user name and password.
	 * 
	 * @param username The username.
	 * @param password The password.
	 * @return
	 */
	public long createUser(User user) {
		ContentValues initialValues = createUserTableContentValues(user);
		System.out.println(initialValues);
		return database.insert(UserConstants.TABLE_USER, null, initialValues);
		
	}
	
	/**
	 * Removes a user's details given an id.
	 * 
	 * @param rowId Column id. 
	 * @return
	 */
	public boolean deleteUser(long rowId) {
		return database.delete(UserConstants.TABLE_USER, UserConstants.COLUMN_ID + "=" + rowId, null) > 0;
	}
	
	public boolean updateUserTable(long rowId, User user) {
		ContentValues updateValues = createUserTableContentValues(user);
		return database.update(UserConstants.TABLE_USER, updateValues, UserConstants.COLUMN_ID + "=" + rowId, null) > 0;
	}
	
	/**
	 * Retrieves the details of all the users stored in the login table.
	 * 
	 * @return
	 */
	public Cursor fetchAllUsers() {
		return database.query(UserConstants.TABLE_USER, new String[] { UserConstants.COLUMN_ID, UserConstants.COLUMN_USERNAME, 
				UserConstants.COLUMN_PW }, null, null, null, null, null);
	}
	
	/**
	 * Retrieves the details of a specific user, given a username and password.
	 * 
	 * @return
	 */
	public Cursor fetchUser(String username, String password) {
		Cursor myCursor = database.query(UserConstants.TABLE_USER, 
				new String[] { UserConstants.COLUMN_ID, UserConstants.COLUMN_USERNAME, UserConstants.COLUMN_PW ,UserConstants.COLUMN_PRIMARY_MAIL,UserConstants.COLUMN_AGE,UserConstants.COLUMN_HOME,UserConstants.COLUMN_SEX}, 
				UserConstants.COLUMN_USERNAME + "='" + username + "' AND " + 
				UserConstants.COLUMN_PW + "='" + password + "'", null, null, null, null);
		
		if (myCursor != null) {
			myCursor.moveToFirst();
		}
		return myCursor;
	}
	
	/**
	 * Returns the table details given a row id.
	 * @param rowId The table row id. 
	 * @return
	 * @throws SQLException
	 */
	public Cursor fetchUserById(long rowId) throws SQLException {
		Cursor myCursor = database.query(UserConstants.TABLE_USER, 
				new String[] { UserConstants.COLUMN_ID, UserConstants.COLUMN_USERNAME, UserConstants.COLUMN_PW }, 
				UserConstants.COLUMN_ID + "=" + rowId, null, null, null, null);
		if (myCursor != null) {
			myCursor.moveToFirst();
		}
		return myCursor;
	}
	
	/**
	 * Stores the username and password upon creation of new login details.
	 * @param username The user name.
	 * @param password The password.
	 * @return The entered values. 
	 */
	private ContentValues createUserTableContentValues(User user) {
		ContentValues values = new ContentValues();
		values.put(UserConstants.COLUMN_USERNAME, user.getUserName());
		values.put(UserConstants.COLUMN_PW, user.getPassword());
		values.put(UserConstants.COLUMN_PRIMARY_MAIL, user.getPrimaryMail());
		values.put(UserConstants.COLUMN_SEX, user.getSex());
		values.put(UserConstants.COLUMN_HOME, user.getHome());
		values.put(UserConstants.COLUMN_AGE, user.getAge());
		return values;
	}
}
